package it.diego.projecttwo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SlidingDrawer;
import android.widget.TextView;
import android.widget.Toast;

public class ContextMenuActivity extends Activity implements OnClickListener{

    private String[] Countries;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Countries = getResources().getStringArray(R.array.Game);
    	ListView list = (ListView)findViewById(R.id.list);
    	ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listitem, Countries);
    	list.setAdapter(adapter);
    	registerForContextMenu(list);
    	
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
//        return(super.onPrepareOptionsMenu(menu));
    }
    
    public boolean onContextItemSelected(MenuItem item) 
    {
    	AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
    	int menuItemIndex = item.getItemId();
    	String[] menuItems = getResources().getStringArray(R.array.contextmenu);
    	String menuItemName = menuItems[menuItemIndex];
    	String listItemName = Countries[info.position];
    	TextView text = (TextView)findViewById(R.id.footer);
    	text.setText(String.format("Selected %s for item %s", menuItemName, listItemName));
    	return true;
    }
    
    public boolean onOptionsItemSelected(MenuItem item) 
    {
    	switch (item.getItemId()) 
    {
    		case R.id.Menu1:
    			Toast.makeText(this, "Add", Toast.LENGTH_SHORT).show();
    			break;
    		case R.id.submenu:
    			Toast.makeText(this, "Send", Toast.LENGTH_SHORT).show();
    			break;
    		case R.id.Menu3:
    			Toast.makeText(this, "Copy", Toast.LENGTH_SHORT).show();
    			break;
    		case R.id.Menu4:
    			Toast.makeText(this, "Delete", Toast.LENGTH_SHORT).show();
    			break;
    		case R.id.menu_settings:
    			Toast.makeText(this, "Settings", Toast.LENGTH_SHORT).show();
    			break;
    		case R.id.submenu1:
    			Toast.makeText(this, "submenu 1", Toast.LENGTH_SHORT).show();
    			break;
    		case R.id.submenu2:
    			Toast.makeText(this, "submenu 2", Toast.LENGTH_SHORT).show();
    			break;
    	}
    	return(super.onOptionsItemSelected(item));
    }
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
    		ContextMenuInfo menuInfo) {
    	// TODO Auto-generated method stub
    	//super.onCreateContextMenu(menu, v, menuInfo);
    	
    	if (v.getId()==R.id.list) 
    	{
    		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
    		menu.setHeaderTitle(Countries[info.position]);
    		String[] menuItems = getResources().getStringArray(R.array.contextmenu);
    		for (int i = 0; i<menuItems.length; i++) 
    		{
    			menu.add(Menu.NONE, i, i, menuItems[i]);
    		}
    	}
    }

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.cambiaTutorial:
			{
				startActivity(new Intent(this, DialogBoxActivity.class));
				break;
			}
			case R.id.list:
			{
				Toast.makeText(this, "cliccata la lista", Toast.LENGTH_SHORT).show();
				break;
			}
		}
	}
}
